package com.metaera.app.ui.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.data.model.InvestFinanceResponse;
import com.metaera.app.ui.activities.FlashNewsDetailsActivity;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.ViewUtils;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

public class InvestFinanceListAdapter extends BaseQuickAdapter<InvestFinanceResponse, BaseViewHolder> {

    public InvestFinanceListAdapter() {
        super(R.layout.item_invest_finance);

//        addChildClickViewIds(R.id.tv_share);

//        setOnItemChildClickListener((adapter, v, position) -> {
//            if (v.getId() == R.id.tv_share) {
//                new XPopup.Builder(v.getContext()).asCustom(new SharePopup(v.getContext())).show();
//            }
//        });

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(FlashNewsDetailsActivity.newInstance(context, getItem(position).getId()));
        });
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, InvestFinanceResponse response) {
        AppCompatTextView tvLikeNumber = holder.getView(R.id.tv_like_number);
        AppCompatTextView tvDenyNumber = holder.getView(R.id.tv_deny_number);
        Resources resources = tvLikeNumber.getContext().getResources();

        View view1 = holder.getView(R.id.view);
        view1.setBackgroundColor(getItemPosition(response) == 0 ? Color.WHITE : Color.parseColor("#DEDEE1"));

        // 利好大于利空显示绿色(#48A988)，反之红色(#D14255)，相平显色灰色(#A0A0A8)
        holder.setText(R.id.tv_title, response.getTitle())
                .setTextColor(R.id.tv_like_number, Color.parseColor(response.getLike_num() > response.getDeny_num() ? "#48A988" : "#A0A0A8"))
                .setText(R.id.tv_like_number, resources.getString(R.string.item_li_hao) + " " + response.getLike_num()) // 利好

                .setTextColor(R.id.tv_deny_number, Color.parseColor(response.getDeny_num() > response.getLike_num() ? "#D14255" : "#A0A0A8"))
                .setText(R.id.tv_deny_number, resources.getString(R.string.item_li_kong) + " " + response.getDeny_num()) // 利空

                .setText(R.id.tv_date, DateUtils.getTime(response.getRelease_time_stamp(), DateUtils.PATTERN_4));

        tvLikeNumber.setCompoundDrawables(
                ViewUtils.getCompoundDrawable(
                        resources,
                        response.getLike_num() > response.getDeny_num()
                                ? R.drawable.b_ic_up_green
                                : R.drawable.b_ic_up_gray
                ),
                null,
                null,
                null
        );

        tvDenyNumber.setCompoundDrawables(
                ViewUtils.getCompoundDrawable(
                        resources,
                        response.getDeny_num() > response.getLike_num()
                                ? R.drawable.b_ic_down_red
                                : R.drawable.b_ic_down_gray
                ),
                null,
                null,
                null
        );
    }

}
